Symfony's commons instruments
First released in October, 2005, by
Fabien Potencier, CEO of
Sensio
Based on 3 key ideas:
MVC: Model View Controller
ORM: Object Relational Mapping
Templating System
Major features
- Easy to install and configure
- Database engine-independent
- Simple to use, in most cases, but still flexible enough
Automated Features
- The built-in internationalization layer
- templates/layouts
- Forms
- Output escaping
- Cache management
- Authentication and credential features
- Routing and smart URLs
- Built-in e-mail and API management
- Plug-ins
- Ajax interactions
Development Tools
- Code-generation
- unit and functional testing.
- Debug panel
- Live configuration changes
- Logging features
- OOP: Object Oriented Programming
- ORM: Object Relational Mapping
- RAD: Rapid Application Development
- KISS: Kip it Simple, Stupid
- TDD: Test-Driven Development
- PEAR: PHP Extension and Application Repository
ORM
Lots of benefits:
- Encapsulation, database engine independece, etc.
- Extendable
public function getTotal()
{
$total = 0;
foreach ($this->getItems() as $item)
{
$total += $item->getPrice() * $item->getQuantity();
}
return $total;
}
public function getName()
{
return $this->getFirstName().' '.$this->getLastName();
}
Rapid Application Development (RAD)
- Clients change their mind continiously
- Need an output as soon as possible
Don't Repeat Yourself (DRY)
Do fragmentation as much as possible
Kiss, Keep it simple, stupid
Albert Einstein: everything should be made as simple as possible, but no simpler
Antoine de Saint Exupéry: It seems that perfection is reached not when there is nothing left to add, but when there is nothing left to take away
Key Classes
sfController, sfResponse, sfRequest
Code Organization
Applications, Modules, and Actions

File Structure
apps
/
frontend
/
backend
/
cache
/
config
/
data
/
sql
/
doc
/
lib
/
model
/
log/
plugins
/
test
/
bootstrap
/
unit
/
functional
/
web
/
css
/
images
/
js
/
uploads
/
Application's file structure
apps/
[application name]/
config/
i18n/
lib/
modules/
templates/
layout.php
Module's file structure
apps/
[application name]/
modules/
[module name]/
actions/
actions.class.php
config/
lib/
templates/
indexSuccess.php
Web folder structure
web/
css/
images/
js/
uploads/
Parameter Holder
$request->getParameterHolder()->set('foo', 'bar');
echo $request->getParameterHolder()->get('foo');
=> 'bar'
or
$request->setParameter('foo', 'bar');
echo $request->getParameter('foo');
=> 'bar'
// The 'foobar' parameter is not defined, so the getter returns an empty value
echo $request->getParameter('foobar');
=> null
// A default value can be used by putting the getter in a condition
if ($request->hasParameter('foobar'))
{
echo $request->getParameter('foobar');
}
else
{
echo 'default';
}
=> default
// But it is much faster to use the second getter argument for that
echo $request->getParameter('foobar', 'default');
=> default
Allowing namespace
$user->setAttribute('foo', 'bar1');
$user->setAttribute('foo', 'bar2', 'my/name/space');
echo $user->getAttribute('foo');
=> 'bar1'
echo $user->getAttribute('foo', null, 'my/name/space');
=> 'bar2'
You can use it
class MyClass
{
protected
$parameterHolder = null;
public function initialize
($parameters = array())
{
$this->parameterHolder = new sfParameterHolder
();
$this->parameterHolder->add($parameters);
}
public function getParameterHolder
()
{
return $this->parameterHolder;
}
}
Constants
// Instead of PHP constants,
define('FOO', 'bar');
echo FOO
;
// symfony uses the sfConfig object
sfConfig
::set('foo', 'bar');
echo sfConfig
::get('foo');
Class Autoloading
include 'classes/MyClass.php';
$myObject = new MyClass();
replaced by
$myObject = new MyClass();